package com.blkj.spring_boot_blkj.service;

import com.blkj.spring_boot_blkj.mapper.WorkFlowMapper;
import com.blkj.spring_boot_blkj.pojo.*;
import com.blkj.spring_boot_blkj.vo.ConnectionDataReturn;
import com.blkj.spring_boot_blkj.vo.NodeDataJson;
import com.blkj.spring_boot_blkj.vo.WorkFlowDataJson;
import com.blkj.spring_boot_blkj.vo.WorkFlowDataReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WorkFlowService {
    @Autowired
    public WorkFlowMapper workFlowMapper;

    public int addWorkFlowData(WorkFlowData workFlowData) {
        if (workFlowData != null) {
            String id = workFlowData.getId();
            workFlowData.setId(id);
            Integer jsonId = workFlowData.getJsonId();
            workFlowData.setJsonId(jsonId);
            BpmnAttr bpmnAttr = workFlowData.getBpmnAttr();
            workFlowMapper.addWorkFlowData(bpmnAttr);
            int bpmnattrId = workFlowMapper.queryLastId();
            workFlowData.setBpmnAttrId(bpmnattrId);
            workFlowMapper.addWorkFlowBpmData(workFlowData);
            int workflwId = workFlowMapper.queryLastId();
            List<NodeData> nodeData = workFlowData.getNodeData();
            for (NodeData nodeDataList : nodeData) {
                NodeData nodeDataFor = new NodeData();
                nodeDataFor.setCandidateUsers(nodeDataList.getCandidateUsers());
                nodeDataFor.setDocumentation(nodeDataList.getDocumentation());
                nodeDataFor.setCandidateGroups(nodeDataList.getCandidateGroups());
                nodeDataFor.setId(nodeDataList.getId());
                nodeDataFor.setHeight(nodeDataList.getHeight());
                nodeDataFor.setFormKey(nodeDataList.getFormKey());
                nodeDataFor.setName(nodeDataList.getName());
                nodeDataFor.setShux(nodeDataList.getShux());
                nodeDataFor.setType(nodeDataList.getType());
                nodeDataFor.setX(nodeDataList.getX());
                nodeDataFor.setY(nodeDataList.getY());
                nodeDataFor.setWidth(nodeDataList.getWidth());
                ServiceTaskRule ServiceTaskRuleFor = new ServiceTaskRule();
                ServiceTaskRule serviceTaskRule = nodeDataList.getServiceTaskRule();
                if(serviceTaskRule!= null){
                    ServiceTaskRuleFor.setType(serviceTaskRule.getType());
                    ServiceTaskRuleFor.setValue(serviceTaskRule.getValue());
                    workFlowMapper.addRuleData(ServiceTaskRuleFor);
                    int ruleID = workFlowMapper.queryLastId();
                    nodeDataFor.setServicetaskruleId(ruleID);
                }
                nodeDataFor.setWorkflowId(workflwId);
                workFlowMapper.addNodeData(nodeDataFor);
            }
            List<ConnectionData> connectionData = workFlowData.getConnectionData();
            for (ConnectionData connectionDataList : connectionData) {
                ConnectionData connection = new ConnectionData();
                connection.setId(connectionDataList.getId());
                connection.setName(connectionDataList.getName());
                connection.setType(connectionDataList.getType());
                connection.setRenderType(connectionDataList.getRenderType());
                connection.setWorkflowId(workflwId);
                Target targetData = new Target();
                Target target = connectionDataList.getTarget();
                targetData.setX(target.getX());
                targetData.setY(target.getY());
                targetData.setElementId(target.getElementId());
                workFlowMapper.addTargetData(targetData);
                int targetId = workFlowMapper.queryLastId();
                connection.setTargetId(targetId);
                Source sourceData = new Source();
                Source source = connectionDataList.getSource();
                sourceData.setX(source.getX());
                sourceData.setY(source.getY());
                sourceData.setElementId(source.getElementId());
                workFlowMapper.addSourceData(sourceData);
                int sourceId = workFlowMapper.queryLastId();
                connection.setSourceId(sourceId);
                workFlowMapper.addConnectionData(connection);
            }
            return 1;
        }
        return 0;
    }

    public List<WorkFlowDataJson> getJsonWorkFlow(){
        List<WorkFlowDataJson> WorkFlowDataJsonList = new ArrayList<>();
        // WorkFlowDataReturn workFlowDataSelect = workFlowMapper.selectWorkFlowData();
        List<WorkFlowDataReturn> workFlowDataSelectList = workFlowMapper.selectWorkFlowData();
        for(WorkFlowDataReturn workFlowDataReturnFor:workFlowDataSelectList){
            WorkFlowDataJson workFlowData = new WorkFlowDataJson();
            workFlowData.setId(workFlowDataReturnFor.getId());
            workFlowData.setJsonId(workFlowDataReturnFor.getJsonId());
            BpmnAttr bpmnAttr = new BpmnAttr();
            BpmnAttr bpmnAttrData = workFlowMapper.selectBpmnAttrData(workFlowDataReturnFor.getBpmnAttrId());
            bpmnAttr.setBpmnName(bpmnAttrData.getBpmnName());
            bpmnAttr.setBpmnDescription(bpmnAttrData.getBpmnDescription());
            bpmnAttr.setProcessBean(bpmnAttrData.getProcessBean());
            workFlowData.setBpmnAttr(bpmnAttr);
            List<NodeData> nodeDataList = workFlowMapper.selectNodeData(workFlowDataReturnFor.getWorkflowId());
            List<NodeDataJson> nodeDataJson = new ArrayList<>();
            for(NodeData nodeDataFor:nodeDataList){
                ServiceTaskRule serviceTaskRule = new ServiceTaskRule();
                NodeDataJson nodeData = new NodeDataJson();
                nodeData.setY(nodeDataFor.getY());
                nodeData.setX(nodeDataFor.getX());
                nodeData.setWidth(nodeDataFor.getWidth());
                nodeData.setType(nodeDataFor.getType());
                nodeData.setName(nodeDataFor.getName());
                nodeData.setHeight(nodeDataFor.getHeight());
                nodeData.setShux(nodeDataFor.getShux());
                nodeData.setFormKey(nodeDataFor.getFormKey());
                nodeData.setCandidateGroups(nodeDataFor.getCandidateGroups());
                nodeData.setDocumentation(nodeDataFor.getDocumentation());
                nodeData.setId(nodeDataFor.getId());
                nodeData.setServiceTaskRule(workFlowMapper.selectTaskRuleData(nodeDataFor.getServicetaskruleId()));
                if(nodeData.getServiceTaskRule()!= null){
                    serviceTaskRule.setType(nodeData.getServiceTaskRule().getType());
                    serviceTaskRule.setValue(nodeData.getServiceTaskRule().getValue());
                    nodeData.setServiceTaskRule(serviceTaskRule);
                }
                nodeData.setCandidateUsers(nodeDataFor.getCandidateUsers());
                nodeDataJson.add(nodeData);
            }
            workFlowData.setNodeData(nodeDataJson);
            List<ConnectionData> connectionData = workFlowMapper.selectConnectionData(workFlowDataReturnFor.getWorkflowId());
            List<ConnectionDataReturn> connectionDataJson = new ArrayList<>();
            for(ConnectionData connectionDataFor:connectionData){
                // ConnectionData connection = new ConnectionData();
                Source source = new Source();
                Target target = new Target();
                ConnectionDataReturn connectionDataReturn = new ConnectionDataReturn();
                connectionDataReturn.setId(connectionDataFor.getId());
                connectionDataReturn.setType(connectionDataFor.getType());
                connectionDataReturn.setRenderType(connectionDataFor.getRenderType());
                connectionDataReturn.setName(connectionDataFor.getName());
                connectionDataReturn.setTarget(workFlowMapper.selectTargetData(connectionDataFor.getTargetId()));
                if(connectionDataReturn.getTarget()!= null){
                    target.setElementId(connectionDataReturn.getTarget().getElementId());
                    target.setX(connectionDataReturn.getTarget().getX());
                    target.setY(connectionDataReturn.getTarget().getY());
                    connectionDataReturn.setTarget(target);
                }
                connectionDataReturn.setSource(workFlowMapper.selectSourceData(connectionDataFor.getSourceId()));
                if(connectionDataReturn.getSource()!= null){
                    source.setElementId(connectionDataReturn.getSource().getElementId());
                    source.setX(connectionDataReturn.getSource().getX());
                    source.setY(connectionDataReturn.getSource().getY());
                    connectionDataReturn.setSource(source);
                }
                connectionDataJson.add(connectionDataReturn);
            }
            //workFlowData.setNodeData(nodeDataJson);
            workFlowData.setConnectionData(connectionDataJson);
            WorkFlowDataJsonList.add(workFlowData);
        }
        return WorkFlowDataJsonList;
    }
}
